package com.castle.fortress.admin.system.service.impl;

import cn.hutool.core.util.StrUtil;

import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.system.entity.SysCountryEntity;
import com.castle.fortress.admin.system.dto.SysCountryDto;
import com.castle.fortress.admin.system.mapper.SysCountryMapper;
import com.castle.fortress.admin.system.service.SysCountryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 全球国家地区信息 服务实现类
 *
 * @author mjj
 * @since 2023-07-01
 */
@Service
public class SysCountryServiceImpl extends ServiceImpl<SysCountryMapper, SysCountryEntity> implements SysCountryService {
    /**
    * 获取查询条件
    * @param sysCountryDto
    * @return
    */
    public QueryWrapper<SysCountryEntity> getWrapper(SysCountryDto sysCountryDto){
        QueryWrapper<SysCountryEntity> wrapper= new QueryWrapper();
        if(sysCountryDto != null){
            SysCountryEntity sysCountryEntity = ConvertUtil.transformObj(sysCountryDto,SysCountryEntity.class);
            wrapper.like(StrUtil.isNotEmpty(sysCountryEntity.getCountryNameCn()),"country_name_cn",sysCountryEntity.getCountryNameCn());
            wrapper.like(StrUtil.isNotEmpty(sysCountryEntity.getCountryNameEn()),"country_name_en",sysCountryEntity.getCountryNameEn());
            wrapper.like(StrUtil.isNotEmpty(sysCountryEntity.getCountryNameEnFull()),"country_name_en_full",sysCountryEntity.getCountryNameEnFull());
            wrapper.like(StrUtil.isNotEmpty(sysCountryEntity.getAlphaCode2()),"alpha_code2",sysCountryEntity.getAlphaCode2());
            wrapper.like(StrUtil.isNotEmpty(sysCountryEntity.getAlphaCode3()),"alpha_code3",sysCountryEntity.getAlphaCode3());
            wrapper.like(StrUtil.isNotEmpty(sysCountryEntity.getCountryCode()),"country_code",sysCountryEntity.getCountryCode());
            wrapper.like(StrUtil.isNotEmpty(sysCountryEntity.getAreaCode()),"area_code",sysCountryEntity.getAreaCode());
            wrapper.like(StrUtil.isNotEmpty(sysCountryEntity.getChinesePinyin()),"chinese_pinyin",sysCountryEntity.getChinesePinyin());
        }
        return wrapper;
    }


    @Override
    public IPage<SysCountryDto> pageSysCountry(Page<SysCountryDto> page, SysCountryDto sysCountryDto) {
        QueryWrapper<SysCountryEntity> wrapper = getWrapper(sysCountryDto);
        Page<SysCountryEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<SysCountryEntity> sysCountryPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<SysCountryDto> pageDto = new Page(sysCountryPage.getCurrent(), sysCountryPage.getSize(),sysCountryPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(sysCountryPage.getRecords(),SysCountryDto.class));
        return pageDto;
    }


    @Override
    public List<SysCountryDto> listSysCountry(SysCountryDto sysCountryDto){
        QueryWrapper<SysCountryEntity> wrapper = getWrapper(sysCountryDto);
        List<SysCountryEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,SysCountryDto.class);
    }
}

